﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.IO;

namespace KeyInfoGetter
{
    public partial class Form1 : Form
    {
        string result = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }

        private string GetCPUInfo()
        {
            string cpuinfo = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuinfo += mo.Properties["ProcessorId"].Value.ToString();
                mo.Dispose();
            }
            moc.Dispose();
            mc.Dispose();
            return cpuinfo;
        }

        private string GetHDDInfo()
        {
            string HDid = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                HDid += mo.Properties["Model"].Value.ToString();
                mo.Dispose();
            }
            moc.Dispose();
            mc.Dispose();
            return HDid;
        }

        private string GetMAC()
        {
            string macInfo = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                    macInfo += mo["MacAddress"].ToString();
                mo.Dispose();
            }
            moc.Dispose();
            mc.Dispose();
            return macInfo;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string keyinfo = GetCPUInfo() + GetHDDInfo() + GetMAC();
            StringBuilder sb = new StringBuilder();
            foreach (char c in keyinfo)
            {
                sb.Insert(0, c);
            }
            string keyinfoReserved = sb.ToString();
            string keyinfoRBased = Convert.ToBase64String(Encoding.UTF8.GetBytes(keyinfoReserved));
            result = keyinfoRBased;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"info.dat";
            string msg = "Done!";
            try
            {
                using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
                {
                    sw.Write(result);
                }
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
            }
            MessageBox.Show(msg);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string msg = "Done!";
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                try
                {
                    string filePath = openFileDialog1.FileName;   //  获得用户选择的文件
                    if (filePath.Substring(filePath.LastIndexOf("\\") + 1).ToLower() == "keyfile.dat")
                    {
                        string srs = string.Empty;
                        using (StreamReader sr = new StreamReader(filePath, Encoding.Default))
                        {
                            srs = sr.ReadToEnd();
                        }
                        StringBuilder sb = new StringBuilder();
                        foreach (char c in srs)
                        {
                            sb.Insert(0, c);
                        }
                        string key = sb.ToString();
                        string path = @"c:\windows\system32\msdate.dll";
                        using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
                        {
                            sw.Write(key);
                        }
                    }
                    else
                    {
                        msg = "File Error!";
                    }
                }
                catch (Exception ex)
                {
                    msg = ex.ToString();
                }
            }
            else
            {
                msg = "Key file not found!!";
            }
            MessageBox.Show(msg);
        }
    }
}
